//
//  TextFieldTestVC.swift
//  CXSwiftDemo
//
//  Created by 潮汐 on 2023/12/12.
//

import UIKit

class TextFieldTestVC: CXBaseViewController, UITextFieldDelegate{
    //懒加载
    lazy var textField = UITextField()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        textField.frame = CGRect(x:30, y:160, width:200, height:30)
        //设置边框样式为圆角矩形
        textField.borderStyle = UITextField.BorderStyle.roundedRect
        
        //修改圆角半径的话需要将masksToBounds设为true
        textField.layer.masksToBounds = true
        textField.layer.cornerRadius = 12.0  //圆角半径
        textField.layer.borderWidth = 2.0  //边框粗细
        textField.layer.borderColor = UIColor.blue.cgColor //边框颜色
        //占位符
        textField.placeholder = "小驴拉磨，坚持不懈"
        self.view.addSubview(textField)
        
        //文字大小超过文本框长度时自动缩小字号，而不是隐藏显示省略号
        textField.adjustsFontSizeToFitWidth = true //当文字超出文本框宽度时，自动调整文字大小
        textField.minimumFontSize = 14  //最小可缩小的字号
        
        //清除按钮（输入框内右侧小叉）
        textField.clearButtonMode = .whileEditing  //编辑时出现清除按钮
        textField.clearButtonMode = .unlessEditing  //编辑时不出现，编辑后才出现清除按钮
        textField.clearButtonMode = .always  //一直显示清除按钮
        //密码输入框
        textField.isSecureTextEntry = true //输入内容会显示成小黑点
        
        
        //水平/垂直对齐方式

        /** 水平对齐 **/
        textField.textAlignment = .right //水平右对齐
        textField.textAlignment = .center //水平居中对齐
        textField.textAlignment = .left //水平左对齐
         
        /** 垂直对齐 **/
        textField.contentVerticalAlignment = .top  //垂直向上对齐
        textField.contentVerticalAlignment = .center  //垂直居中对齐
        textField.contentVerticalAlignment = .bottom  //垂直向下对齐

        
        //设置文本框关联的键盘类型
        textField.keyboardType = UIKeyboardType.URL
        
        
        //设置键盘 return 键的样式
        textField.returnKeyType = UIReturnKeyType.done //表示完成输入
        textField.returnKeyType = UIReturnKeyType.go //表示完成输入，同时会跳到另一页
        textField.returnKeyType = UIReturnKeyType.search //表示搜索
        textField.returnKeyType = UIReturnKeyType.join //表示注册用户或添加数据
        textField.returnKeyType = UIReturnKeyType.next //表示继续下一步
        textField.returnKeyType = UIReturnKeyType.send //表示发送
        
        //首字母是否大写
        textField.autocapitalizationType = UITextAutocapitalizationType.none
        //键盘外观
        textField.keyboardAppearance = UIKeyboardAppearance.default
        
        //使文本框在界面打开时就获取焦点，并弹出输入键盘
        textField.becomeFirstResponder()
        //使文本框失去焦点，并收回键盘
        textField.resignFirstResponder()
    }
    
    //代理方法
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
         print("将要开始编辑")
         return true
     }
     
     func textFieldDidBeginEditing(_ textField: UITextField) {
         print("已经开始编辑")
     }
     
     func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
         print("将要结束编辑")
         return true
     }
     
     func textFieldDidEndEditing(_ textField: UITextField) {
         print("已经结束编辑")
     }
     
     func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
         print("文本输入内容将要发生变化（每次输入都会调用）")
         return true
     }
     
     func textFieldShouldClear(_ textField: UITextField) -> Bool {
         print("将要清除输入内容，返回值是是否要清除掉内容")
         return true
     }
     
    //键盘 return 键的响应
     func textFieldShouldReturn(_ textField: UITextField) -> Bool {
         print("将要按下Return按钮，返回值是是否结束输入（是否失去焦点）")
         return true
     }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
